package com.zwz.service.baseConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zwz.bean.SysResourceBean;
import com.zwz.bean.Tree;
import com.zwz.dao.BaseDao;
import com.zwz.dao.SysResourceDao;
import com.zwz.dao.SysRoleResourceDao;
import com.zwz.model.SysResource;
import com.zwz.model.SysRoleResource;
import com.zwz.service.BaseService;

@Service
public class SysResourceService extends BaseService<SysResource> {
	@Autowired
	private SysResourceDao<SysResource> sysResourceDao;
	@Autowired
	private SysRoleResourceDao<SysRoleResource> sysRoleResourceDao;
	
	@Override
	public BaseDao<SysResource> getDao() {
		return sysResourceDao;
	}
	/**
	 * 查询左侧菜单
	 * @param sysResource
	 * @return
	 */
	public List<SysResource> findAllResourceByRole(SysResourceBean sysResource) {
		List<SysResource> sysResourceParentList = sysResourceDao.findAllResourceByRole(sysResource);
		for (SysResource resource : sysResourceParentList) {
			SysResourceBean res=new SysResourceBean();
			res.setPid(resource.getId());//父菜单
			res.setRole_id(sysResource.getRole_id());//角色ID
			res.setRows(10000);
			List<SysResource> sysResourceChildList = sysResourceDao.findAllResourceByRole(res);
			resource.setSysResourceChild(sysResourceChildList);
		}
		return sysResourceParentList;
	}
	
	/**
	 * 查询资源列表
	 * @param sysResource
	 * @return
	 */
	public List<SysResource> findAllPage(SysResourceBean sysResource) {
		List<SysResource> list=new ArrayList<SysResource>();
		int rowCount=sysResourceDao.findCountPage();
		sysResource.getPager().setRowCount(rowCount);
		sysResource.setRows(rowCount);
		List<SysResource> sysResourceMenuList=sysResourceDao.findAllPage(sysResource);
		list.addAll(sysResourceMenuList);
		//查询按钮
		for (SysResource res : sysResourceMenuList) {
			if(res.getResource_type()!="2"){
				SysResourceBean sys=new SysResourceBean();
				sys.setPid(res.getId());
				sys.setResource_type("1");
				List<SysResource> sysResourceSubMenuList=sysResourceDao.findAllById(sys);
				list.addAll(sysResourceSubMenuList);
			}
		}
		return list;
	}
	/**
	 * 查询菜单节点
	 * @return
	 */
	public List<Map<String, Object>> findAllPid(){
		List<Map<String, Object>> listmap=new ArrayList<Map<String,Object>>();
		//查询主菜单
		List<Map<String, Object>> listPid = sysResourceDao.findAllPid("0");
		//查询子菜单
		for (Map<String, Object> map : listPid) {
			Map<String, Object> paraMap=new HashMap<String, Object>();
            map.put("pid", map.get("id"));
            map.put("resource_type", "0");
			List<Map<String, Object>> listSubMap = sysResourceDao.findAllSubPid(paraMap);
			map.put("children", listSubMap);
			listmap.add(map);
		}
		return listmap;
	}
	/**
	 * 查询查询三级树
	 * @return
	 */
	public List<Tree> findAllTrees(){
		List<Tree> treeOneList=new ArrayList<Tree>();
		//查询一级菜单
		List<Map<String, Object>> resourcesList = sysResourceDao.findAllPid("0");
		for (Map<String, Object> resourceOne : resourcesList) {
			Tree treeOne = new Tree();
			treeOne.setId(String.valueOf(resourceOne.get("id")));
            treeOne.setText(String.valueOf(resourceOne.get("text")));
            treeOne.setIconCls(String.valueOf(resourceOne.get("iconCls")));
            //查询二级菜单
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("pid", resourceOne.get("id"));
            map.put("resource_type", "0");
            List<Map<String, Object>> listSubMap = sysResourceDao.findAllSubPid(map);
            if(CollectionUtils.isEmpty(listSubMap)){
            	treeOne.setState("open");
            }else{
            	List<Tree> treeTwoList = new ArrayList<Tree>();
            	for (Map<String, Object> resourceTwo : listSubMap) {
            		Tree treeTwo = new Tree();
            		treeTwo.setId(String.valueOf(resourceTwo.get("id")));
            		treeTwo.setText(String.valueOf(resourceTwo.get("text")));
            		treeTwo.setIconCls(String.valueOf(resourceTwo.get("iconCls")));
                    //查询按钮
            		Map<String, Object> paraMap=new HashMap<String, Object>();
            		paraMap.put("pid", resourceTwo.get("id"));
            		paraMap.put("resource_type", "1");
                    List<Map<String, Object>> listSonsMap = sysResourceDao.findAllSubPid(paraMap);
                    if(CollectionUtils.isEmpty(listSonsMap)){
                    	treeTwo.setState("open");
                    }else{
                    	List<Tree> treeThreeList = new ArrayList<Tree>();
                    	for (Map<String, Object> resourceThree : listSonsMap) {
                    		Tree treeThree = new Tree();
                    		treeThree.setId(String.valueOf(resourceThree.get("id")));
                    		treeThree.setText(String.valueOf(resourceThree.get("text")));
                    		treeThree.setIconCls(String.valueOf(resourceThree.get("iconCls")));
                    		
                    		treeThreeList.add(treeThree);
						}
                    	treeTwo.setChildren(treeThreeList);
                    }
                    treeTwoList.add(treeTwo);
				}
            	treeOne.setChildren(treeTwoList);
            }
            treeOneList.add(treeOne);
		}
		return treeOneList;
	}
	
	/**
	 * 根据角色ID查询资源ID列表
	 * @param roleId
	 * @return 资源ID
	 */
	public List<String> findRoleResourceId(String roleId){
		return sysRoleResourceDao.findRolesResourceId(roleId);
	}
	
	/**
	 * 保存权限-角色の资源
	 * @param ids
	 * @param roleId
	 */
	public void saveAuth(String[] ids,String roleId)throws Exception{
		//删除该角色下的全部权限
		sysRoleResourceDao.deleteAllByRoleId(roleId);
		//批量保存
		if(ids!=null&&ids.length>0){
			Map<String, Object> map=new HashMap<String,Object>();
			map.put("roleId", roleId);
			map.put("resource_ids", ids);
			sysRoleResourceDao.addBatchRoleAndResource(map);
		}
	}
	
	/**
	 * 查询全部链接
	 * @return
	 */
	public List<Map<String, String>> findAllUrl(){
		return sysResourceDao.findAllUrl();
	}
}
